﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace AbingtonBooks
{
    public sealed class ShoppingCart
    {
        private ShoppingCart() { }
        private static ShoppingCart instance;
        private decimal total = 0;

        //public struct Item {
        //    public int quantity { get; set; }
        //    public string itemNumber { get; set; }
        //    public decimal price { get; set; }
        //    public string bookTitle { get; set; }
        //}

        private List<Book> items = new List<Book>();

        public static ShoppingCart getShoppingCart() {
            if (instance == null)
            {
                instance = new ShoppingCart();
               
            }
            return instance;
        }
        public void addToCart(Book myBook) {

            int index = items.FindIndex(b => b.BookId == myBook.BookId);
            if (index == -1)
            {
                items.Add(myBook);
                total += myBook.Price;
            }
        }

        public List<Book> Items
        {
            get { return items; }
        }
        public decimal Total
        {
            get { return total; }
        }
        public void removeItemFromCart(Book book) {
          int index = items.FindIndex(b => b.BookId == book.BookId);

           
            if (index>-1)
            {
                total -= book.Price;
                items.RemoveAt(index);
            }

        }
        public void emptyCart() {
            items = null;
            total = 0;
        }
       
    }
}